<template>
  <div class="app-container">
    <div class="filter-container">
      <el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-edit" @click="resetFormData">
        添加节点
      </el-button>
    </div>
    <el-table
      v-loading="listLoading"
      :data="list"
      style="width: 100%"
      row-key="id"
      border
    >
      <el-table-column type="expand">
        <template slot-scope="{row}">
          <el-tabs active-name="first" type="border-card">
            <el-tab-pane label="节点参数" name="first">
              <FlowNodeParamsList :flow-id="flowId" :node-id="row.id" />
            </el-tab-pane>
            <el-tab-pane label="关联子流程" name="last">
              <FlowJoinList :flow-id="flowId" :node-id="row.id" />
            </el-tab-pane>
          </el-tabs>
        </template>
      </el-table-column>
      <el-table-column
        prop="sort"
        label="节点序号"
      />
      <el-table-column
        prop="name"
        label="节点名称"
      />
      <el-table-column
        prop="nodeCode"
        label="节点标识"
      />
      <el-table-column
        label="节点类型"
      >
        <template slot-scope="{row}">
          {{ row.type | exchange }}
        </template>
      </el-table-column>
      <el-table-column
        prop="roleCode"
        label="操作权限组代码"
      />
      <el-table-column label="操作" width="240" align="center">
        <template slot-scope="{row}">
          <el-button type="success" @click="updateFormData(row)">编辑节点 </el-button>
          <el-button type="danger" @click="deleteNode(row)">删除节点 </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog :title="title" :visible.sync="dialogFormVisible" style="width: 1100px;margin: auto">
      <el-form ref="dataForm" :rules="rules" :model="fromTemp" label-position="left" label-width="90px" style="width: 400px; margin-left:50px;">
        <el-form-item label="节点序号" prop="sort">
          <el-input v-model="fromTemp.sort" />
        </el-form-item>
        <el-form-item label="节点名称" prop="name">
          <el-input v-model="fromTemp.name" />
        </el-form-item>
        <el-form-item label="节点标识" prop="nodeCode">
          <el-input v-model="fromTemp.nodeCode" />
        </el-form-item>
        <el-form-item label="节点类型" prop="name">
          <el-select v-model="fromTemp.type" placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="权限组编号" prop="roleCode">
          <el-input v-model="fromTemp.roleCode" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" style="margin-right: 50px">
        <el-button @click="dialogFormVisible = false">
          取消
        </el-button>
        <el-button type="primary" @click="createUser">
          确认
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { DeleteFlowNode, GetFlowNodeList, SaveFlowNode } from '@/api/SystemManagerApi'
import FlowNodeParamsList from '@/views/SystemSetting/SystemFlow/components/FlowNodeParamsList'
import FlowJoinList from '@/views/SystemSetting/SystemFlow/components/FlowJoinList'

const optionsMap = {
  'START': '开始节点',
  'NODE': '审批节点',
  'CREATE_NODE': '发起人处理节点',
  'END': '结束节点',
  'FLOW': '子流程'
}

export default {
  components: { FlowNodeParamsList, FlowJoinList },
  filters: {
    exchange(val) {
      return optionsMap[val]
    }
  },
  props: ['flowId'],
  data() {
    return {
      rules: {
        sort: [{ required: true, message: 'type is required', trigger: 'change' }],
        name: [{ required: true, message: 'type is required', trigger: 'change' }]
      },
      title: '编辑节点',
      fromTemp: {
        id: null,
        name: '',
        flowId: this.flowId,
        sort: 0,
        type: '',
        roleCode: ''
      },
      dialogFormVisible: false,
      listLoading: false,
      list: [],
      userId: null,
      updateList: null,
      options: [
        { value: 'START', label: '开始节点' },
        { value: 'NODE', label: '审批节点' },
        { value: 'CREATE_NODE', label: '发起人处理节点' },
        { value: 'END', label: '结束节点' },
        { value: 'FLOW', label: '子流程' }
      ]
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      GetFlowNodeList(this.flowId).then(response => {
        this.list = response.data
        this.listLoading = false
      })
    },
    resetFormData() {
      this.fromTemp = {
        id: null,
        name: '',
        flowId: this.flowId,
        sort: 0,
        type: '',
        nodeCode: '',
        roleCode: ''
      }
      this.dialogFormVisible = true
    },
    updateFormData(row) {
      this.fromTemp = Object.assign({}, row)
      this.dialogFormVisible = true
    },
    createUser() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          SaveFlowNode(this.fromTemp).then((response) => {
            this.dialogFormVisible = false
            this.$notify({
              title: 'Success',
              message: 'Created Successfully',
              type: 'success',
              duration: 2000
            })
            this.getList()
          })
        }
      })
    },
    deleteNode(row) {
      this.$confirm('此操作将永久删除该, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        DeleteFlowNode(row.id).then((res) => {
          this.getList()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>
